package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.EmployeeAccount;
import com.zrwl.poscloud.base.entity.EmployeeAccountLog;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DoubleComputeUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.EmployeeAccountLogMapper;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消费人账户变动 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-07-27
 */
@Service
public class EmployeeAccountLogServiceImpl extends SuperServiceImpl<EmployeeAccountLogMapper, EmployeeAccountLog> implements EmployeeAccountLogService {

    @Autowired
    private EmployeeAccountLogService employeeAccountLogService;
    @Autowired
    private EmployeeAccountService employeeAccountService;

    @Resource
    private EmployeeAccountLogMapper employeeAccountLogMapper;

    @Override
    public IPage<EmployeeAccountLog> getPageList(EmployeeAccountLogVO employeeAccountLogVO) {
        QueryWrapper<EmployeeAccountLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(employeeAccountLogVO.getKeyword()) && !StringUtils.isEmpty(employeeAccountLogVO.getKeyword().trim())) {
            queryWrapper.like("name", employeeAccountLogVO.getKeyword().trim());
        }
        if (employeeAccountLogVO.getBeginTime() != null) {
            queryWrapper.gt("create_time", employeeAccountLogVO.getBeginTime());
        }
        if (employeeAccountLogVO.getEndTime() != null) {
            queryWrapper.le("create_time", employeeAccountLogVO.getEndTime());
        }
        if (StringUtils.isNotEmpty(employeeAccountLogVO.getEmployeeUid())) {
            queryWrapper.eq("employee_uid", employeeAccountLogVO.getEmployeeUid());
        }
        if (StringUtils.isNotEmpty(employeeAccountLogVO.getAccountUid())) {
            queryWrapper.eq("account_uid", employeeAccountLogVO.getAccountUid());
        }
        if (employeeAccountLogVO.getType() != 0) {
            queryWrapper.eq("type", employeeAccountLogVO.getType());
        }
        Page<EmployeeAccountLog> page = new Page<>();
        page.setCurrent(employeeAccountLogVO.getCurrentPage());
        page.setSize(employeeAccountLogVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<EmployeeAccountLog> pageList = employeeAccountLogService.page(page, queryWrapper);
        List<EmployeeAccountLog> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }


    @Override
    public IPage<EmployeeAccountLogVO> getEmployeeAccountLogList(EmployeeAccountLogVO employeeAccountLogVO) {
        Page<EmployeeAccountLog> page = new Page<>();
        page.setCurrent(employeeAccountLogVO.getCurrentPage());
        page.setSize(employeeAccountLogVO.getPageSize());
        IPage<EmployeeAccountLogVO> pageList = employeeAccountLogMapper.getEmployeeAccountLogList(page, employeeAccountLogVO);
        List<EmployeeAccountLogVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<EmployeeAccountLogVO> getTradeDetailAccountLogList(EmployeeAccountLogVO employeeAccountLogVO) {
        Page<EmployeeAccountLog> page = new Page<>();
        page.setCurrent(employeeAccountLogVO.getCurrentPage());
        page.setSize(employeeAccountLogVO.getPageSize());
        IPage<EmployeeAccountLogVO> pageList = employeeAccountLogMapper.getTradeDetailAccountLogList(page, employeeAccountLogVO);
        List<EmployeeAccountLogVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<EmployeeAccountLogVO> getRechargeTradeDetailAccountLogList(EmployeeAccountLogVO employeeAccountLogVO) {
        Page<EmployeeAccountLog> page = new Page<>();
        page.setCurrent(employeeAccountLogVO.getCurrentPage());
        page.setSize(employeeAccountLogVO.getPageSize());
        IPage<EmployeeAccountLogVO> pageList = employeeAccountLogMapper.getRechargeTradeDetailAccountLogList(page, employeeAccountLogVO);
        List<EmployeeAccountLogVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<EmployeeAccountLog> getAllList(EmployeeAccountLogVO employeeAccountLogVO) {
        QueryWrapper<EmployeeAccountLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<EmployeeAccountLog> list = employeeAccountLogService.list(queryWrapper);
        return list;
    }

    @Override
    public boolean add(EmployeeAccountLogVO employeeAccountLogVO) {
        EmployeeAccountLog entity = new EmployeeAccountLog();
        BeanUtils.copyProperties(employeeAccountLogVO, entity, SysConf.STATUS);
        entity.insert();
        employeeAccountLogVO.setUid(entity.getUid());
        return true;
    }

    @Override
    public String edit(EmployeeAccountLogVO employeeAccountLogVO) {
        EmployeeAccountLog entity = employeeAccountLogService.getById(employeeAccountLogVO.getUid());
        BeanUtils.copyProperties(employeeAccountLogVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(EmployeeAccountLogVO employeeAccountLogVO) {
        EmployeeAccountLog entity = employeeAccountLogService.getById(employeeAccountLogVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String generateEmployeeAccountLog(List<TradeDetailVO> tradeDetailVOList) throws Exception {
        for (TradeDetailVO items : tradeDetailVOList) {
            if (items.getSubAccountType() == 0) {
                if (items.getExPurseConsume() > 0) {
                    employeeAccountEdit(items, 1);//电子钱包
                }
            } else if (items.getSubAccountType() == 1) {
                if (items.getExPurseConsume() > 0) {
                    employeeAccountEdit(items, 1);//电子钱包
                }
                if (items.getSubAccountConsume() > 0) {
                    employeeAccountEdit(items, 2);//补贴账户1
                }
            } else if (items.getSubAccountType() == 2) {
                throw new RuntimeException("不支持2账户操作");
//                employeeAccountEdit(items, 1);//电子钱包
//                employeeAccountEdit(items, 3);//补贴账户2
            }
        }
        return ResultUtil.successWithMessage("采集完成");
    }

    void employeeAccountEdit(TradeDetailVO tradeDetailVO, int type) {//0数据风险已处理
        EmployeeAccountVO employeeAccountVO = new EmployeeAccountVO();
        employeeAccountVO.setEmployeeUid(tradeDetailVO.getEmployeeUid());
        employeeAccountVO.setType(type);
        EmployeeAccount employeeAccount = employeeAccountService.customizeSelect(employeeAccountVO);
        EmployeeAccountLog entity = new EmployeeAccountLog();
        entity.setAccountUid(employeeAccount.getUid());
        entity.setSourceUid(tradeDetailVO.getUid());
        entity.setSourceType(5);//卡机业务默认
        Integer count = employeeAccount.getRecordNo();
        entity.setRecordNo(count);
        entity.setBeforeBalance(employeeAccount.getBalance());
        if (type == 1 || type == 0) {
            entity.setAmt(tradeDetailVO.getExPurseConsume());
        } else {
            entity.setAmt(tradeDetailVO.getSubAccountConsume());
        }
        if (tradeDetailVO.getType() == 0) {//人工消费机退款
            entity.setSummary(tradeDetailVO.getSummary() + entity.getAmt());
            entity.setType(1);//增款
        }
        if (tradeDetailVO.getType() == 8) {
            entity.setSummary("POS机业务-刷卡退款：" + entity.getAmt());
            entity.setType(1);//增款
        }
        if (tradeDetailVO.getType() == 3) {
            entity.setSummary("POS机业务-充值：" + entity.getAmt());
            entity.setType(1);//增款
        }
        if (tradeDetailVO.getType() == 9) {//人工操作撤销
            entity.setSummary(tradeDetailVO.getSummary() + entity.getAmt());
            entity.setType(2);//减款
        }
        if (tradeDetailVO.getType() == 10) {
            entity.setSummary("POS机业务-充值退款：" + entity.getAmt());
            entity.setType(2);//减款
        }
        if (tradeDetailVO.getType() == 1) {
            entity.setSummary("POS机业务-刷卡消费：" + entity.getAmt());
            entity.setType(2);//减款
        }
        entity.setStatus(1);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.setCreateTime(new Date());
        entity.insert();
        if (tradeDetailVO.getType() == 8 || tradeDetailVO.getType() == 3 || tradeDetailVO.getType() == 0) {//其他卡需要生成补贴指令
            employeeAccount.setBalance(DoubleComputeUtils.sum(employeeAccount.getBalance(), entity.getAmt()));
        }
        if (tradeDetailVO.getType() == 1 || tradeDetailVO.getType() == 10 || tradeDetailVO.getType() == 9) {//其他卡需要生成代扣指令
            employeeAccount.setBalance(DoubleComputeUtils.sub(employeeAccount.getBalance(), entity.getAmt()));
        }
        employeeAccount.setRecordNo(employeeAccount.getRecordNo() + 1);
//        employeeAccount.setUpdateTime(new Date());
        employeeAccount.updateById();
        if (employeeAccount.getBalance() < 0) {//账户扣款产生负账加入黑名单(单SN号识别需要更正)
            ICCardAuthListVO icCardAuthListVO = new ICCardAuthListVO();
            icCardAuthListVO.setCardSerialNumber(tradeDetailVO.getCardSerialNumber());
            icCardAuthListVO.setEmployeeUid(tradeDetailVO.getEmployeeUid());
            icCardAuthListVO.setCompanyUid(tradeDetailVO.getCompanyUid());
//            iCCardAuthListService.systemAddBlackAuth(icCardAuthListVO);
        }
    }

    @Override
    public EmployeeAccountLogVO insertRechargeBill(Map<String, String> row, EmployeeAccountVO employeeAccountVO, EmployeeVO employeeVO, boolean accountWeight) {
        /**
         * 产生一条充值账单(导入充值)
         * 账户变动记录(电子钱包的变动前余额和变动后余额)
         * 变更账户余额
         * 补贴账户产生一条充值记录
         */
        return null;
    }

}
